import {commUrl} from '@/config/common.js'
export const request = (options) => {
	return new Promise((resolve, reject) => {
		let token = '';
		if (uni.getStorageSync('token')) {
			token = uni.getStorageSync('token');
		}
		let header={'Content-Type': 'application/json'}
		if(token){
			// header.token = token
			header['authorization'] = "Bearer " + token
		}

		
		uni.request({
			url: decodeURI(encodeURI(commUrl+options.url).replace(/%E2%80%8B/g,"")),
			method: options.method || 'GET',
			data: options.data || {},
			header,
			sslVerify: false,
			success: (res) => {
				console.log(res)
				if (res.statusCode == 500 ) {
					uni.showToast({
						title: (res?.data?.message)?res?.data?.message:'服务开小差了，请联系管理员！',
						icon:"none"
					})
					reject(res)
				} else {

					if (res.statusCode == 200) {
						resolve(res.data)
					} else {
						uni.hideLoading();
						uni.showToast({
							title: res.data?.message??'服务器开小差了！请联系管理员！',
							icon:"none"
						})
						reject(res.data)
						
					}
				}
			},
			fail: (err) => {
				console.log(err)
				uni.showToast({
					title: '接口请求失败',
					icon:"none"
				})
				reject(err)
			}
		})
	})
}
